/******************************************************************************
 *                                                                            *
 *               (c) 2008-2009 HutLab. All rights reserved.                   *
 *                        Eduardo Henrique Leite                              *
 *                                                                            *
 ******************************************************************************

 History:
 Created 05/01/2009

 */

package hutlab.sudoku.engine.ai;

/**
 *
 */
public class SudokuPuzzle {

    // ----- FIELDS -----

    private SudokuCell[][] mPuzzle;
    private int mSize = 9;
    private int mUnsolvedCells;

    // ----- GETTERS -----

    public SudokuCell[][] getPuzzle(){
        return mPuzzle;
    }

    public int getSize(){
        return mSize;
    }

    public int getUnsolvedCells(){
        return mUnsolvedCells;
    }

    // ----- PUBLIC -----

    public void setValue(int row, int col, int value){
        boolean mark = false;

        if (this.mPuzzle[row][col].getValue() == 0){
            mark = true;
        }

        this.mPuzzle[row][col].setValue(value);

        for (int i = 0; i < 9; i++) {
            this.mPuzzle[row][i].removePossibility(value);
            this.mPuzzle[i][col].removePossibility(value);

            int rowRegion = (row / 3) * 3 + (i / 3);
            int colRegion = (col / 3) * 3 + (i % 3);

            this.mPuzzle[rowRegion][colRegion].removePossibility(value);
        }

        if (mark) {
            this.mUnsolvedCells--;
        }
    }

    // ----- CONSTRUCTOR -----

    public SudokuPuzzle() {
        this.mPuzzle = new SudokuCell[this.mSize][this.mSize];

        this.mUnsolvedCells = this.mSize * this.mSize;

        for (int i = 0; i < this.mSize; i++) {
            this.mPuzzle[i] = new SudokuCell[this.mSize];
            for (int j = 0; j < this.mSize; j++) {
                    this.mPuzzle[i][j] = new SudokuCell(i,j);
                }
            }
        }
}
